//引入express模块
const express = require('express')
//引入数据库连接模块
const c = require('../connection')
//创建路由器对象
const router = express.Router()

//  登录验证接口

router.get('/yz',(req,res,next)=>{
    var obj=req.query
    console.log(obj);
    c.query('select * from cs_login where username=?',[obj.username],(err,r)=>{
        if(err){
            return next(err)
        }
        if(r.length>0){
            res.send({
                code:200,
                msg:'该账户存在',
                data:r
            })
        }else{
            res.send({
                code:401,
                msg:'未找到该用户',
                data:r
            })
        }
    })
})

//  登录接口

router.post('/test', (req, res, next) => {
    var obj = req.body
    console.log(obj);
    c.query('select * from cs_login where username=? && pwd=?', [obj.username, obj.pwd], (err, r) => {
        if (err) {
            return next(err)
        }
        if (r.length !=0) {
            res.send({
                code: 200,
                msg: '登录帐号',
                data: r
            })
        }else{
            res.send({
                code:404,
                msg:'没有该登录帐号',
                data:r
            })
        }
    })
})
//  传图片接口
router.post('/tt',(req,res)=>{
    var obj=req.body
    c.query(`insert into cs_pic set ? `,[obj],(err,r)=>{
        if(err){
            throw err
        }
        console.log(r);
        res.send({
            code:200,
            msg:'传输成功',
            data:r
        })
    })
})
router.get('/ttt',(req,res)=>{
    c.query(`select * from cs_pic`,(err,r)=>{
        if(err){
            throw err
        }
        if(r.length>0){
            res.send({
                code:200,
                msg:'查询成功',
                data:r
            })
        }
    })
})
module.exports = router